# coding=UTF-8
import datetime
import json

import schedule

from httputils import feige_req as feige
from httputils import weather_req as weather


def job1():
    print('------------------------------------------------------------------------')
    print('Job1:每隔10秒执行一次的任务,查看脚本运行状态......')
    print('Job1-startTime:%s' % (datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')))
    print('Job1-endTime:%s' % (datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')))


def job2():
    print('------------------------------------------------------------------------')
    print('Job2:每天上午06:05执行一次,发送天气预报信息')
    print('Job2-startTime:%s' % (datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')))
    res = weather.weather_req()

    # 解析响应信息
    json_loads = json.loads(res)
    info = json_loads['天气']
    data = ['小雨', '中雨', '大雨', '暴雨', '小雪', '中雪', '大雪', '暴雪', '阴']

    # 判断是否需要发送报警信息
    if info in data:
        """发送报警信息"""
        event_name = "天气预报提醒"
        event_num = datetime.datetime.now().strftime('%Y%m%d%H%M%S')
        event_type = "坏天气提醒"
        event_person = "小鱼儿"
        event_detail = "今天天气：【" + info + "】"
        event_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        event_remark = "请做好防护措施！"
        res = feige.send_req(event_name, event_num, event_type, event_person, event_detail, event_time, event_remark)
        print(res)
    else:
        """打印天气详情"""
        print(res)
        print('Job2-endTime:%s' % (datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')))
        print('------------------------------------------------------------------------')


def job3():
    print('------------------------------------------------------------------------')
    print('Job3:每周六上午06:05执行一次,企业微信发送【本周上线内容】')
    print('Job3-startTime:%s' % (datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')))

    # 发送内容
    first = "工作周报提醒"
    keyword1 = "统计本周【上线内容】"
    keyword2 = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    remark = "统计本周所有上线内容,发送到工作群中"

    # 请求接口
    res = feige.send_qywx_rep(first=first, keyword1=keyword1, keyword2=keyword2, remark=remark)
    print(res)
    print('Job3-endTime:%s' % (datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')))


'''
  python中模块和函数的区别:
  在python中调用函数是先要导入模块（module）再选择导入的模块中的哪一个函数（function）
'''
if __name__ == '__main__':
    schedule.every(10).seconds.do(job1)
    schedule.every().day.at('06:05').do(job2)
    schedule.every().saturday.at('06:05').do(job3)
    while True:
        schedule.run_pending()
